<script setup>
import { ref } from 'vue';
import { showToast } from 'vant';
import {useRouter} from "vue-router";

const router = useRouter()

// 已选标签
const show = ref(true);
const close = (id) => {
  activeIds.value.splice(id,1)
};
const activeIds = ref([]);
const activeIndex = ref(0);
// 全部标签
const originTagList = [
  {
    text: '性别',
    children: [
      { text: '男', id: '男' },
      { text: '女', id: '女' },
    ],
  },
  {
    text: '年纪',
    children: [
      { text: '大一', id: '大一' },
      { text: '大二', id: '大二' },
      { text: '大三', id: '大三' },
      { text: '大四', id: '大四' },
    ],
  },
];

// 标签搜索
let tagList = ref(originTagList)
const searchText = ref('')
const onCancel = () => {
  searchText.value = ''
  tagList.value = originTagList
}
// 搜索框
const onSearch = (val) => {
  tagList.value = originTagList.map(parentTag => {
    const tempChildren = [...parentTag.children]
    const tempParentTag = {...parentTag}
    tempParentTag.children = tempChildren.filter(item => item.text.includes(searchText.value))
    showToast(searchText.value)
    return tempParentTag
  })
}

const doSearchResult = () => {
  router.push({
    path: '/user/list',
    query: {
      tags: activeIds.value || null
    }
  })
}
</script>

<template>
  <form action="/public">
    <van-search
        v-model="searchText"
        show-action
        placeholder="请输入搜索标签关键词"
        @search=" onSearch"
        @cancel="onCancel"
    />
  </form>
  <div v-if="activeIds.length > 0">
    <van-divider  content-position="left">已选标签</van-divider>
    <van-row>
      <van-col v-for="(tag,index) in activeIds">
        <van-tag style="margin: 2px" @close="close(index)"  :show="show" closeable type="primary" size="medium">{{tag}}</van-tag>
      </van-col>
    </van-row>

  </div>

  <van-divider content-position="left">全部标签</van-divider>
  <van-tree-select
      v-model:active-id="activeIds"
      v-model:main-active-index="activeIndex"
      :items="tagList"
  />

  <div class="add-button">
    <van-button block type="primary" @click="doSearchResult">搜索</van-button>
  </div>
</template>

<style scoped>
.add-button{
  padding: 20px;
}
</style>